
Application = function(project, _callback){
	if (!project) project = {title:"Sample Application", erd:[{entities:[]}]};
	
	var app_data		= {};
	app_data.title		= project.name;
	app_data.data		= {};
	app_data.menu		= [];
	app_data.settings	= [];


	var container	= $('<div class="fd-app-container" style="display:inline-block;">').appendTo($("body"));
	var p_hdr		= $("<div style='width:100%'>").text(project.name).appendTo(container);
	var p_content	= $("<div style='width:100%;height:100%'>").appendTo(container);
	var menu		= $('<div class="fd-app-menu">').appendTo(p_content);
	var content		= $('<div class="fd-app-content">').appendTo(p_content);
	
	var message		= $('<div class="fd-app-message">')
						.addClass("ui-border-all ui-widget-header")
						.css({
							  "width"		: "100%"
							, "height"		: "20%"
							, "overflow"	: "auto"
						})
						//.hide()
						.appendTo(menu);
	
	container.css({
		  width		: "800"
		, height	: "600"
		, position	: "fixed"
		, top		: 40
		, left		: 40
	});
	
	content.css({
		  width		: "79%"
		, height	: "100%"
		, top		: 0
		, float		: "right"
		, display	: "inline-block"
	});
	
	menu.css({
		  width				: "20%"
		, height			: "75%"
		, top				: 0
		, float				: "left"
		, "border-right"	: "2px solid black"
	});
	
	menu.resizable({
		  minHeight		: container.height()*0.75
		, maxHeight		: container.height()*0.75
		, handles		: "e"
	});
	
	menu.resize(function(){
		var cw = parseFloat(container.css("width"));
		var mw = parseFloat(menu.css("width"));
		var w = cw - mw - 5;
		
		content.css("width", w);
	});
	
	var menu_hdr = $("<div>")
						.addClass("ui-widget-header ui-corner-top")
						.css({"width":"100%", "text-align": "center", "padding-top": "5px", "padding-bottom": "5px"})
						.appendTo(menu)
						.text("Menu");
	
	var menu_body = $("<ul>")
						.addClass("ui-widget-header")
						.css({
							  "width"			: "100%"
							, "height"			: "100%"
							, "list-style"		: "none"
							, "padding"			: "0"
							, "overflow-y"		: "auto"
							, "overflow-x"		: "hidden"
							})
						.appendTo(menu);
	
	$(project.erd[0].entities).each(function(){
		var li = $("<li id='"+this.form_code.name+"' ><a href='#'>"+this.form_code.title+"</a></li>");
		
		switch(this.form_code.app_category){
			case "settings":
			case "hidden": 
				break;
			
			default:
				li.appendTo(menu_body);
		}
	});

	var li = $("<li id='fd-app-settings-page' ><a href='#' class='ui-border-top'><span class='ui-icon ui-icon-gear' style='float:left;'/>Settings</a></li>").appendTo(menu_body);
	
	menu_body.menu({
		select	: function(evt, ui){
				var obj = null;
				var id = ui.item[0].id;
				
				if ("fd-app-settings-page" == id){
					message
						.html(
							"<h2>Settings</h2>"+
							"<h3>Control some settings for your application</h3>"
						)
						.show();
					return;
				}
				
				$(project.erd[0].entities).each(function(){
					if (this.form_code.name == id)
						obj = this;
				});
				
				message
					.html(
						"<h2>"+obj.form_code.title+"</h2>"+
						"<h3>"+obj.form_code.comment+"</h3>"
					)
					.show();
				
				obj.eventName		= null;
				obj.ajax_url		= null;
				obj.content_area	= content;
				obj.upload_url		= "boiler_plate/fileupload.php";
				obj.toolbar			= obj.form_code.toolbar;
				obj.alignment		= obj.form_code.ui_alignment;
				
				$(project.erd[0].entities).each(function(){
					cache[this.form_code.name] = this.data;
				});
				
				$().lookup_form(obj, function(data){
					//console.log("setting data for: "+obj.form_code.name);
					$(project.erd[0].entities).each(function(){
						if (this.form_code.name == ui.item[0].id){
							this.data = data;
						}
					});
				});

			}
	});
	
	container.show();

	container.fd_panel({
		  title			: project.name
		, draggable		: true
		, canClose		: true
		, canResize		: true
		, onClose		: function(){ if (_callback) _callback(project);}
	});
};
